<?php
/**
 * 魔杖2.0接口工具
 *
 */
namespace Moxie;
class Moxie
{
    
    public function getMoZhangContent( $name ,$idcard ,$mobile ,$qq_number='',$method)
    {  
        $app_id = "c636c784c34544a485be58e47e85b54d";
//    	$method = "moxie.api.risk.magicwand2.application";//调接口时间传入
        $format = "JSON";
        $sign_type = "RSA";
        $version = "1.0";
        $timestamp = $this->getMillionSeconds();
        $biz_content = '{"name":"'.$name.'","idcard":"'.$idcard.'","mobile":"'.$mobile.'"}';
		$paramsStr = "app_id={$app_id}&biz_content={$biz_content}&format={$format}&method={$method}&sign_type={$sign_type}&timestamp={$timestamp}&version={$version}";
        //rsa私钥字符串
        $secret = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCZZ9Ed32gTHkLXG/CrNtcqnIP2vfeh+P4eWtL10oOGqALtI7sMtq+pcOv94j6b60t3+ByFc7WCHdefusBCAUIPN+jkPQTZzV8QJTqt2ZEeEoe8HxwD8Y6sH+PPgRiiMJBKXmST3XQbNc0A/Dl8VGuK2PRzU8R6RoL4B3lDRsfIHwSiJPkkBRp6M47hiig5yuWlFAhCIwKWwcIr6NGrhv0RB46MZm3C1+QYDJ5pbGY4oh4y57SPbrZW5TrvnRb37e67PU+Tm6nyKYu8Yeb0Y8wWKZGbTYIY1lmubvevxnhSUJxIb2drKH2UpL1wCdn+Whn061xtOO3VIYsM2xMphlLhAgMBAAECggEBAIGFQu0q4+9IXHzMLU4RtmdW0KByUfMijEPKwVUl0UDO9/GKenJIiFXSbp3j+KKibgyzojkWschTM+u7vHm+IC1DMlIKJt9R45J5LGr+R+URFiNCTOATgjdXkXtwbwD5acowmlCx6EbVDOppCowrpsULWpFIx5nJkOijPsILr8AJByaDL3CLQzy5R2Ibh73Xq7xpy9BX096x9elqDm3ykC9WfFMsFyq2eA1yWPzC37N35DyyZ7cGuaCnkDBdNwV/8fFEqg1B+WQeoPKXZFeYD9HpiUolLb0bNEIKEdyu7Rm1QO2iuzVICGyo/yGNQ0AKo6nQfp+uVJEB9ZI9rA1QAmUCgYEA4EvKHbzW85AyKqvSWnhR7b2AGPnGvRPe2JtkBO9B+yfbEYNt9LqQuupIXVbVIQ4+VNkkjyhaK2oSmOZDyhCfZ8icmGzpdjqpjzAcPv8tBDSt/QhHRrleYVNQ03F+ezcUUllL/QMT2df8L5fQnb9nqKYGKNjuaUJTDhbgwre4XOsCgYEArxbV7qx/E2KzN2Zq0tMgH1OwMUi90P6Fo31DZSlEH4j1EGkFKDxG+0P0IKdsKaGp6YoR0FP07TIO1+iA0Ydx81GW10c2FT76iKlX+jtJHSOAcSHE7c7JtcY7/z78RGhYJGipE7ItllSm6HK4YMaNH3VEXgSEYqn+CqXENc9GLGMCgYAHJyOY8y6/sbeH2Nbqf6/NLx/oq/6EhR+0NLBp399wzQKVemWFSnXXICWSpHRDi5q9Rfd+gUDu5YEWUzGSPhl3S9SCn+aKGLDTmissbyLWYX+y9BKh1MOHwwpu082+zDDft21O8kwtxE+MjQDgJvx9Sl4j61cfEDhp2JzuWMGOZQKBgQCnZRy5qJ+BqC6Ru9BLqeAfM0LTi6k2o/CeowcJM4/W2nj57H4qvp5aYdiIU6j8kHxDNGbdlzcmg2JfmAF40ZbnO00Mmx+g5mpeuBDNw1qeii+ledSuS7LboCACY3SJ7ny+qnVjyEj/XgWJXQzcMVgBCHuJq6XqMn4YLe3OSFHd4wKBgCV0tIXe99UFyBPTUE88WeK6zN5EAGWcJkUX7GiJRL4QJ78EETebQP//q2HOtowaqbnHFJznE+xx5ncdCcCuA/elnOF6lnHPyNvRSSP9Ypr0A4VQC4mUtsfqLBs4eWEQB9fHWqjj3CLSPQ3NoLlnPfhWiMOa7ehJA4w5iyQzeJAy";
        //获取签名
        $sign = $this->getSign($paramsStr,$secret);
//		p($sign);die;
        $url = 'https://api.51datakey.com/risk-gateway/api/gateway?'.$paramsStr."&sign=".$sign;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HTTPGET, true);
        $content = curl_exec($curl);
        if(curl_errno($curl))
        {
            $content =  curl_error($curl);
        }
        curl_close($curl);
        return $content;
    }
    
    /**
     * 获取SHA1签名
     */
    public function getSign($paramsStr,$secret)
    {
        $signature = "";
        $str = chunk_split( $secret, 64, "\n" );
        $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n";
        openssl_sign( $paramsStr  ,$signature ,$key  );
        
        return base64_encode($signature);
    }
    
    /**
     * 获取毫秒级时间戳
     * @return number
     */
    protected function getMillionSeconds()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        
        return $msectime ;
    }

    
}